﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DataAccess;
using BLL;

namespace QuanLyKhachSan
{
    public partial class TabPhanQuyen : XtraUserControl,IGeneatetabName
    {
        private List<Quyen> _quyen1 = new List<Quyen>();
        private ActionResult _action = new ActionResult();
        private NhomTaiKhoan _nhomTaiKhoan = new NhomTaiKhoan();
        private int _macDinh = 1;

        public TabPhanQuyen()
        {
            InitializeComponent();
        }

        public string Getname()
        {
            return "Phân Quyền";
        }

        public void LayQuyen()
        {
            _quyen1 = new QuyenManager().LayDanhSach().Result;
            foreach (var lis in _quyen1)
                treeView1.Nodes.Add(lis.TenQuyen);
        }

        private void LayTk()
        {
            gridControlTaiKhoan.DataSource = new TaiKhoanManager().LayDanhSach().Result;
        }

        private void LayNhomTk()
        {
            gridControlNhomTK.DataSource = new NhanVienManager().LayNhomTk().Result;
        }

        private void TabPhanQuyen_Load(object sender, EventArgs e)
        {
            LayQuyen();
            LayTk();
            LayNhomTk();
        }

        private void ChekedFalse(bool typ)
        {
            _macDinh = 0;
            int index = treeView1.Nodes.Count;
            for (int i = 0; i < index; i++)
                treeView1.Nodes[i].Checked = typ;
            _macDinh = 1;
        }

        private void btnThemQuyen_Click(object sender, EventArgs e)
        {
            var frm = new FormQuyenCapNhat();
            frm.ShowDialog();
            if (frm.Success)
            {
                XtraMessageBox.Show("Thêm mới quyền thành công", "Thông báo", MessageBoxButtons.OK);
                treeView1.Nodes.Add(frm.Quyen.TenQuyen);
            }
        }

        private void btnXoaQuyen_Click(object sender, EventArgs e)
        {
            var tn = treeView1.SelectedNode;
            if (tn != null)
            {
                var inum = new QuyenManager().LayDanhSach().Result.Where(q => q.TenQuyen.Contains(tn.Text));
                var lis = inum.ToList();
                _action = new QuyenManager().Xoa(lis[0]);
                if (_action.Sucess())
                {
                    XtraMessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK);
                    treeView1.Nodes.Remove(tn);
                    var pq = new PhanQuyenManager().LayDanhSach().Result.Where(q => q.MaQuyen.Equals(lis[0].MaQuyen)).ToList();
                    for (int i = 0; i < pq.Count; i++)
                        _action = new PhanQuyenManager().Xoa(pq[i]);
                }
                else
                    MessageBox.Show(_action.GetErrorMessage());
            }
        }

        private NhomTaiKhoan GetNhomTk()
        {
            _nhomTaiKhoan = gridView1.GetRow(gridView1.GetSelectedRows()[0]) as NhomTaiKhoan;
            return _nhomTaiKhoan;
        }


        private TreeNode GetTreeNode(string text)
        {
            var tn = new TreeNode();
            for (int i = 0; i < treeView1.Nodes.Count; i++)
            {
                tn = treeView1.Nodes[i];
                if (tn.Text.Equals(text))
                    return tn;
            }
            return null;
        }
        private void gridView1_Click(object sender, EventArgs e)
        {
            ChekedFalse(false);
            try
            {
                _macDinh = 0;
                var nhomTk = GetNhomTk();
                gridControlTaiKhoan.DataSource = new TaiKhoanManager().LayDanhSach().Result.Where(tk => tk.MaNhom.Equals(nhomTk.MaNhom)).ToList();
                var listQuyen = new PhanQuyenManager().LayDanhSach().Result.Where(pq => pq.MaNhom.Equals(nhomTk.MaNhom)).ToList();
                var quyen = new Quyen();
                TreeNode tn;
                for (int i = 0; i < listQuyen.Count; i++)
                {
                    quyen = new QuyenManager().LayDanhSach().Result.Where(q => q.MaQuyen.Equals(listQuyen[i].MaQuyen)).ToList()[0];
                    tn = GetTreeNode(quyen.TenQuyen);
                    tn.Checked = true;
                }
            }
            catch { }
            _macDinh = 1;
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (_macDinh == 1)
            {
                var tn = treeView1.SelectedNode;
                if (tn == null)
                {
                    XtraMessageBox.Show("Chưa chọn quyền, không thể thực hiện phân quyền!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    ChekedFalse(false);
                    _nhomTaiKhoan = null;
                    return;
                }
                if (_nhomTaiKhoan == null)
                {
                    XtraMessageBox.Show("Chưa chọn nhóm tài khoản, không thể thực hiện phân quyền!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    ChekedFalse(false);
                    return;
                }
                var action = new ActionResult();
                var pq = new PhanQuyen();
                pq.MaNhom = _nhomTaiKhoan.MaNhom;
                pq.MaQuyen = new QuyenManager().LayDanhSach().Result.Where(q => q.TenQuyen.Equals(tn.Text)).ToList()[0].MaQuyen;
                if (tn.Checked)
                    action = new PhanQuyenManager().Them(pq);
                else
                    action = new PhanQuyenManager().Xoa(pq);
                if (action.Sucess())
                    XtraMessageBox.Show("Phân quyền thành công", "Thông báo", MessageBoxButtons.OK);
            }
            _macDinh = 0;
        }

        private void resetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayTk();
        }
    }
}
